package com.dd.web.base.controller;

import com.dd.common.entity.CommonResult;
import com.dd.web.base.entity.common.CommonPage;
import com.dd.web.base.entity.data.UmsDictData;
import com.dd.web.base.entity.view.DictDataVo;
import com.dd.web.base.entity.view.DictListParam;
import com.dd.web.base.service.UmsDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 数据字典
 */
@RestController
@RequestMapping("/base/dict/data")
@Api(tags = "UmsDictDataController", value = "数据字典管理")
public class UmsDictDataController {

    @Autowired
    private UmsDictDataService dictDataService;

    @ApiOperation("查询字典数据列表")
    @GetMapping
    public CommonPage<DictDataVo> list(DictListParam param){
        return dictDataService.selectDictDataList(param);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/{dictType}")
    @ApiOperation("根据字典类型获取字典数据")
    @ApiImplicitParam(paramType = "path",value="字典类型编码",dataType = "String",required = true)
    public CommonResult<List<UmsDictData>> queryDataByType(@NotBlank(message="字典类型编码为空")
                                                               @PathVariable String dictType)
    {
        return CommonResult.success(dictDataService.selectDictDataByType(dictType));
    }

    @ApiOperation("批量停用字典数据")
    @PutMapping(value="/stop/{ids}")
    public CommonResult<Boolean> doStopData(@PathVariable Long[] ids){
        int rowCount = this.dictDataService.doStopDictData(ids);
        return CommonResult.success(rowCount>0);
    }

    @ApiOperation("批量启用字典数据")
    @PutMapping(value="/start/{ids}")
    public CommonResult<Boolean> doStartData(@PathVariable Long[] ids){
        int rowCount = this.dictDataService.doStratDictData(ids);
        return CommonResult.success(rowCount>0);
    }



    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典数据")
    @PostMapping
    public CommonResult<Integer> add(@RequestBody DictDataVo dictData)
    {
        return CommonResult.success(dictDataService.insertDictData(dictData));
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation( "修改字典数据")
    @PutMapping
    public CommonResult<Integer> edit(DictDataVo dictData)
    {
        dictData.setUpdateBy("");
        return CommonResult.success(dictDataService.updateDictData(dictData));
    }

    /**
     * 删除字典
     */
    @ApiOperation("删除字典")
    @DeleteMapping("/{id}")
    public CommonResult<Integer> remove( Long id)
    {
        return CommonResult.success(dictDataService.deleteDictDataById(id));
    }
}
